<?php
namespace Admin\Controller;
use Lib\Controller\BaseController;

class PublicController extends BaseController {
    
    /**
     * 显示后台登陆页
     */
    public function login() {
//         session('test',1);
//         p($_SESSION);
        //存在登录凭证，则跳转
        //P(U('', '', '', false, true,true));
        //p($_SERVER['HTTP_HOST'] );
//         p(phpinfo());
        //session(C('USER_AUTH_KEY'),NULL);
//         p($_SERVER['HTTP_HOST']);return;
//         session(C('USER_AUTH_KEY'),1);
        if (session(C('USER_AUTH_KEY'))) {
//             p('Location:http://'.$_SERVER['HTTP_HOST']);return;
            //header('Location: '.U('Admin/', '', '', false, true,true));
            //header('Location: '.U('Index/index', '', '', false, true,true));
            header('Location:http://'.$_SERVER['HTTP_HOST']);
        } else {
            $this->display();
        }
    }
    
    public function check_login() {
//         p(I('post.'));p(session());
        //跳转路径
    	$jumpUrl = U("Public/login",'', '', false, true,true);
        if (IS_POST) {
                $secode = session('secode');
                $username = I('username', '');
                $password = I('password', '');
                $verify = I('verify', '');
                
                if (empty($verify)) {
                    $this->tyAjaxReturn(0, '验证码不能为空', $jumpUrl);
                }
                if (empty($username) || empty($password)) {
                    $this->tyAjaxReturn(0, '用户名或密码不能为空', $jumpUrl);
                }
                
//                 $this->verify = new VerifyController();
//                 p($this->verify->check_verify($verify));return;
                if (strcmp(strtoupper($verify),$secode['verify_code'])) {//
                    $this->tyAjaxReturn(-1, '验证码错误，请重新输入', $jumpUrl);
                }
                $returnData = self::_loginAdmin($username, $password); 
                //$this->ajaxReturn($returnData);
                if ($returnData['status'] > 0) {
                        $adminInfo = $returnData['adminInfo'];
                        //保存用户信息
                        session(C('USER_AUTH_KEY'), $adminInfo['id']);
                        session('roleId', $adminInfo['role_id']);
                        session('username', $adminInfo['admin_name']);
                        session('secode', NULL); //注销验证码
                        //保存代理商信息
                        if($adminInfo['role_id'] == '5'){
                            $priceWhere['agent_name']            = session('username');
                            $agentData                           = D('Agent')->where($priceWhere)->find();
                            if($agentData){
                                cookie('agentData',$agentData);
                            }
                        }
//                         p(session());return;
                        $this->tyAjaxReturn(1, L('登录成功'));
                } else {
                    $returnInfo = '';
                    switch ($returnData['status']) {
                        case 0: //表单重复提交
                            $returnInfo = L('表单重复提交');
                            break;
                        case -1: //用户不存在
                            $returnInfo = L('用户不存在');
                            break;
                        case -2: //用户被禁用
                            $returnInfo = L('用户被禁用');
                            break;
                        case -3: //密码不正确
                            $returnInfo = L('密码不正确');
                            break;
                        default:
                            $returnInfo = L('未知错误');
                    }
                    
                    $this->tyAjaxReturn(-2, $returnInfo, $jumpUrl);
                 }
        }else {
            $this->redirect(MODULE_NAME.'/Public/login', null, 0, '', true);
        }
        //p(session('secode'));
    }
      
    /**
     * 管理员登录
     * @param type $username
     * @param type $password
     * @return boolean
     */
    private function _loginAdmin($username, $password) {
        //获取管理员信息
        $bbsAdmin = M('ServerAdmin');    
        $returnInfo = array('status' => 1);
        $adminInfo = $bbsAdmin->where(array('admin_name' => $username))->find();
//         p($adminInfo);return;
        if (!$adminInfo) {
            //用户不存在
            $returnInfo['status'] = -1;
        } else {
//             if ($adminInfo['status'] == 0) {
//                 //用户被禁用
//                 $returnInfo['status'] = -2;
//             } else {
                //验证密码
                $password = md5($password);
                if ($password !== $adminInfo['admin_password']) {
                    //密码不正确
                    $returnInfo['status'] = -3;
                }
//             }
        }
        if ($returnInfo['status'] > 0) {
            //去除敏感信息
            unset($adminInfo['admin_password']);
        
            $returnInfo['adminInfo'] = $adminInfo;
        }
        
        
        return $returnInfo;
    }
    
    
}

?>